﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BestLifePOS_Core.DomainObjects;
using BestLifePOS_Core.Data.Fascade;

namespace BestLifePOS_PresentationLayer
{
    public partial class frmPRApproval : Form
    {
        DataTable GvDatatable1 = new DataTable();
        DataTable GvDatatable = new DataTable();
        DataTable GvPLIDList = new DataTable();

        public frmPRApproval()
        {
            InitializeComponent();
            DataGridViewCheckBoxColumn check = new DataGridViewCheckBoxColumn();
            check.HeaderText = "Check Item";
            check.FalseValue = "0";
            check.TrueValue = "1";
            check.Width = 20;
            dgvPRApproval.Columns.Insert(0, check);

            DataGridViewCheckBoxColumn check1 = new DataGridViewCheckBoxColumn();
            check1.HeaderText = "Check Item";
            check1.FalseValue = "0";
            check1.TrueValue = "1";
            check1.Width = 20;
            dgvPOdetailsapproval.Columns.Insert(0, check1);


            #region GvDatatable-Data Table

            GvDatatable.Columns.Add("Pr_ID");
            GvDatatable.Columns.Add("Qty");
            GvDatatable.Columns.Add("PL_ID");
            GvDatatable.Columns.Add("Status");
            GvDatatable.Columns.Add("Last_Modify");
            GvDatatable.Columns.Add("User_Modify");
            GvDatatable.Columns.Add("Description");
            GvDatatable.Columns.Add("Pro_ID");
            GvDatatable.Columns.Add("Brand");
            GvDatatable.Columns.Add("Pro_Name");
            GvDatatable.Columns.Add("Pur_Price");
            GvDatatable.Columns.Add("PV");
            GvDatatable.Columns.Add("BV");
            GvDatatable.Columns.Add("Ret_Price");
            #endregion


            #region GvPLIDList-Data Table

            GvPLIDList.Columns.Add("Pr_ID");
            GvPLIDList.Columns.Add("PL_ID");
           
            #endregion
        }

        private void frmPRApproval_Load(object sender, EventArgs e)
        {
            PendingPR();
        }




        //First Grid
        public void PendingPR()
        {
            try
            {
                List<Purchase_Request> LvList = new List<Purchase_Request>();
                String LvPenList = "PENDING";
                LvList = Purchase_RequestDAO.GetAllPRbyStatus(LvPenList);
                DataTable LvPRPending = new DataTable();
                dgvPRApproval.DataSource = LvPRPending;

                #region LvPRPending--Datatable
                //LvPRPending.Columns.Add("Select/Unselect", (typeof(bool)));
                LvPRPending.Columns.Add("Pr_Id");
                LvPRPending.Columns.Add("Description");
                LvPRPending.Columns.Add("Total");
                LvPRPending.Columns.Add("status");
                LvPRPending.Columns.Add("Branch_Id");
                LvPRPending.Columns.Add("User_Id");
                LvPRPending.Columns.Add("Accept_Date");
                //LvPRPending.Columns.Add("Po_Id");



                #endregion


                foreach (Purchase_Request Lvitem in LvList)
                {
                    LvPRPending = (DataTable)dgvPRApproval.DataSource;
                    LvPRPending.Rows.Add(LvPRPending.NewRow());
                    LvPRPending.Rows[LvPRPending.Rows.Count - 1][0] = Lvitem.Pr_Id.ToString();
                    LvPRPending.Rows[LvPRPending.Rows.Count - 1][1] = Lvitem.Description.ToString();
                    LvPRPending.Rows[LvPRPending.Rows.Count - 1][2] = Lvitem.Total.ToString();
                    LvPRPending.Rows[LvPRPending.Rows.Count - 1][3] = Lvitem.status.ToString();
                    LvPRPending.Rows[LvPRPending.Rows.Count - 1][4] = Lvitem.Branch_Id.ToString();
                    LvPRPending.Rows[LvPRPending.Rows.Count - 1][5] = Lvitem.User_Id.ToString();
                    LvPRPending.Rows[LvPRPending.Rows.Count - 1][6] = Lvitem.Accept_Date.ToString();

                    //GvPRPending.Rows[7][7]=Lvitem
                }


            }
            catch (Exception)
            {

            }
        }

        private void dgvPRApproval_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewCheckBoxCell ch1 = new DataGridViewCheckBoxCell();

            ch1 = (DataGridViewCheckBoxCell)dgvPRApproval.Rows[dgvPRApproval.CurrentRow.Index].Cells[0];

            string LvPRid = dgvPRApproval.Rows[e.RowIndex].Cells[1].Value.ToString();


            if (ch1.Value == null)

                ch1.Value = false;

            switch (ch1.Value.ToString())
            {

                case "True":

                    break;

                case "False":
                    PRDetailFillGrid(LvPRid);//Fill Second Grid


                    break;

            }



        }


        //Second Grid
        private void PRDetailFillGrid(string PPRid)
        {
            string LvStatus = "PENDING";
            GvDatatable1 = Purchase_RequestDAO.GetAllPRDetailsbyPrid(PPRid, LvStatus);


            for (int i = 0; i <= GvDatatable1.Rows.Count - 1; i++)
            {
                GvDatatable.Rows.Add(GvDatatable.NewRow());
                GvDatatable.Rows[GvDatatable.Rows.Count - 1][0] = GvDatatable1.Rows[i][0].ToString();
                GvDatatable.Rows[GvDatatable.Rows.Count - 1][1] = GvDatatable1.Rows[i][1].ToString();
                GvDatatable.Rows[GvDatatable.Rows.Count - 1][2] = GvDatatable1.Rows[i][2].ToString();
                GvDatatable.Rows[GvDatatable.Rows.Count - 1][3] = GvDatatable1.Rows[i][3].ToString();
                GvDatatable.Rows[GvDatatable.Rows.Count - 1][4] = GvDatatable1.Rows[i][4].ToString();
                GvDatatable.Rows[GvDatatable.Rows.Count - 1][5] = GvDatatable1.Rows[i][5].ToString();
                GvDatatable.Rows[GvDatatable.Rows.Count - 1][6] = GvDatatable1.Rows[i][6].ToString();
                GvDatatable.Rows[GvDatatable.Rows.Count - 1][7] = GvDatatable1.Rows[i][7].ToString();
            }

            dgvPOdetailsapproval.DataSource = GvDatatable;

        }

        private void dgvPOdetailsapproval_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewCheckBoxCell ch1 = new DataGridViewCheckBoxCell();

            ch1 = (DataGridViewCheckBoxCell)dgvPOdetailsapproval.Rows[dgvPOdetailsapproval.CurrentRow.Index].Cells[0];

            string LvPLid = dgvPOdetailsapproval.Rows[e.RowIndex].Cells[3].Value.ToString();
             //string LvStatus = dgvPOdetailsapproval.Rows[e.RowIndex].Cells[4].Value.ToString();
            string LvPRid = dgvPOdetailsapproval.Rows[e.RowIndex].Cells[1].Value.ToString();
            if (ch1.Value == null)

                ch1.Value = false;

            switch (ch1.Value.ToString())
            {

                case "True":

                    break;

                case "False":
                    PLListToApprove(LvPRid, LvPLid);//Add to List


                    break;
            }
        }



        #region Save
        private void PLListToApprove(string PPrID,string PPLid)
        {
            
            GvPLIDList.Rows.Add(GvPLIDList.NewRow());
            GvPLIDList.Rows[GvPLIDList.Rows.Count - 1][0] = PPrID;
            GvPLIDList.Rows[GvPLIDList.Rows.Count - 1][1] = PPLid;
          
        }

        private void btnPRAprovalSubmit_Click(object sender, EventArgs e)
        {
            DataSet ds = new DataSet("podetails");

            string detailsXML = "";

            ds.Tables.Add(GvPLIDList.Copy());
            detailsXML = ds.GetXml();

            PRApprovalList(detailsXML);
            MessageBox.Show("PO Was Succesfuly Saved");

        }

        private void PRApprovalList(string xmlstring)
        {


              string Lvxml = xmlstring;
            string Lvstatus=null;
            if (rbCancel.Checked)
	{
		 Lvstatus="CANCELLED";
	}
            else
                if (rbApproved.Checked)
	{
		  Lvstatus="APPROVED";
	}

            int Lvresult = Purchase_RequestDAO.UpdatePR(Lvxml, Lvstatus);
        
            
            
            if (Lvresult != 0 && Lvstatus == "APPROVED")
        {
            MessageBox.Show("Purchase Request Approved Sucessfully");
        }
            else
                 if (Lvresult != 0 && Lvstatus == "CANCELLED")
        {
            MessageBox.Show("Purchase Request Cancelled Sucessfully");
        }
        }

        #endregion


    }
}



